new Vue({
    el: '#app',
    data(){
        return {
            registrations:[],
            doctors:[],// 医生列表
            activeIndex: '4',
            searchByCondition:{}, // 条件查询
            pickerOptions: {
                disabledDate(time) {
                  return time.getTime() > Date.now();
                },
                shortcuts: [{
                  text: '今天',
                  onClick(picker) {
                    picker.$emit('pick', new Date());
                  }
                }, {
                  text: '昨天',
                  onClick(picker) {
                    const date = new Date();
                    date.setTime(date.getTime() - 3600 * 1000 * 24);
                    picker.$emit('pick', date);
                  }
                }, {
                  text: '一周前',
                  onClick(picker) {
                    const date = new Date();
                    date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                    picker.$emit('pick', date);
                  }
                }]
              }, // 日期快捷
              addForm:{},// 添加挂号单
              dialogVisibleDoctor:false,
              addFormVisible: false, // 添加表达显示
                          // 规则
            rules:{
                patientName: [
                    { required: true, message: '请输入患者名称', trigger: 'blur' },
                    ],
                    patientId: [
                    { required: true, message: '请输入患者身份证号', trigger: 'blur' },
                    ],
                    gender: [
                    { required: true, message: '请选择性别', trigger: 'blur' },
                    ],
                    doctorId: [
                        { required: true, message: '请输入医生身份证号', trigger: 'blur' },
                        ],
                symptoms: [
                { required: true, message: '请输入主要症状', trigger: 'blur' },
                ],
                time: [
                    { required: true, message: '请选择日期', trigger: 'blur' },
                    ],
                    officeVisFee: [
                        { required: true, message: '请输入问诊费', trigger: 'blur' },
                        ],
                        drugFee: [
                            { required: true, message: '请输入药品费', trigger: 'blur' },
                            ],
                },

                doctor:{}, // 医生信息
                formLabelWidth: '120px',
        }
    },
    methods: {
                // 关闭
                handleClose(done) {
                    this.$confirm('确认关闭？')
                      .then(_ => {
                        done();
                      })
                      .catch(_ => {});
                  },
        // 导航
        handleSelect(key, keyPath) {
            if(key==2){
                 location.href="doctor.html"
            }else if(key==1){
                location.href="patient.html"
            }else if(key==3){
                location.href="bill.html"
            }
           
          },

        // 条件查询
        getByCondition(){
            let _this = this;
            console.log(_this.searchByCondition)
            axios(
                {
                    method:"POST",
                    url: "http://localhost/registrations/get/condition",
                    data:_this.searchByCondition
                }
            ).then(function(resp){
                console.log(resp.data);
                _this.registrations=resp.data.data;
                _this.registrations.forEach(item=>{
                    item.time=_this.toDate( item.time)
                })
            })
             },


        // 添加挂号单信息
        add() {
        let _this = this;
        console.log(this.addForm.name)
        if(this.addForm.patientName==undefined|this.addForm.patientId==undefined|this.addForm.gender==undefined|this.addForm.doctorId==undefined|this.addForm.symptoms==undefined|this.addForm.time==undefined|this.addForm.officeVisFee==undefined|this.addForm.drugFee==undefined){
            _this.$message({
                message: "请填写完整信息",
                type:"error"
            })
            return;
        }
        axios(
            {
                method:"POST",
                url: "http://localhost/core/add",
                data:_this.addForm
            }
        ).then(function(resp){
            console.log(resp.data);
            _this.getAllRegistrations();
            _this.addFormVisible=false;
            if(resp.data.success){
            _this.$message({
                message: resp.data.message,
                type:"success"
            })
            }else{
            _this.$message({
                message: resp.data.message,
                type:"error"
            })
            }

        }).catch(function(resp){
            _this.$message({
                message: "身份证格式错误",
                type:"error"
            })
        })
        },
             
        // 获得所有挂号单
        getAllRegistrations(){
            let _this = this;
            axios(
                {
                    method:"GET",
                    url: "http://localhost/registrations/get/all/",
                }
            ).then(function(resp){
                console.log(resp.data);
                _this.registrations=resp.data.data;
                _this.searchByCondition={}
                _this.registrations.forEach(item=>{
                    item.time=_this.toDate( item.time)
                })
            })
        },
// 获得所有医生
getAllDoctors(){
    let _this = this;
    axios(
        {
            method:"GET",
            url: "http://localhost/doctors/all/",
        }
    ).then(function(resp){
        console.log(resp.data);
        _this.doctors=resp.data.data;
    })
},
        // 时间戳转换
        toDate(time){
            var date= new Date(time)
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            var D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
            let strDate = Y+M+D;
            return strDate;
        },         
        // 根据医生id查询医生信息
        getDoctorById(id){
           this.dialogVisibleDoctor=true
           console.log(id)
           let _this = this;
           axios(
               {
                   method:"GET",
                   url: "http://localhost/doctors/get/id/"+id,
               }
           ).then(function(resp){
               console.log(resp.data);
               _this.doctor=resp.data.data;
           })
        },

        // 根据患者id查询挂号单
        getRegistrationsByPatientId(id){
            let _this = this;
            axios(
                {
                    method:"GET",
                    url: "http://localhost/registrations/get/patientid/"+id,
                }
            ).then(function(resp){
                console.log(resp.data);
                _this.registrations=resp.data.data;
                _this.registrations.forEach(item=>{
                    item.time=_this.toDate( item.time)
                })
            })
        },

            // 根据医生id查询挂号单
            getRegistrationsByDoctorId(id){
                let _this = this;
                axios(
                    {
                        method:"GET",
                        url: "http://localhost/registrations/get/doctorid/"+id,
                    }
                ).then(function(resp){
                    console.log(resp.data);
                    _this.registrations=resp.data.data;
                    _this.registrations.forEach(item=>{
                        item.time=_this.toDate( item.time)
                    })
                })
              },
      },




      mounted(){
        var param = location.search;
        if(param==""){
            this.getAllRegistrations()
        }
        console.log(param)
        type=param.slice(1,param.indexOf("="))
        id=param.slice(param.indexOf("=")+1)
        console.log(type=="doctorId")
        if(type=="patientId"){
            this.getRegistrationsByPatientId(id)
        }else if(type=="doctorId"){
            this.getRegistrationsByDoctorId(id)
        }
        this.getAllDoctors();



    }
})